/**
 * Created by 王健 on 2017/8/31.
 */

$(function () {
    var live_data = {
        baseConfig: baseConfig,
        live_program_id: GetQueryString("program_id"),
        goodsstore_id: GetQueryString("goodsstore_id"),
        type: GetQueryString("type"),
        ask: GetQueryString("ask"),
        prgoram: {},
        show_img: true,
        goods_list: [],
        cart_list: [],
        video_list: [],
        goods_ids: [],
        is_all: false,
        is_follow: false,
        goods_order_list: [],
    };
    var width = 0;
    var height = 0;
    if (!live_data.type) {
        live_data.type = 'android'
    }
    live_data.page = GetQueryString('show');
    if (!live_data.page) {
        live_data.page = 'video';
    }
    var a = document.getElementById("live_id");
    if (a) {
        a.value = 'live_id:' + live_data.live_program_id;
    }


    var app = new Vue({
        el: '#play_background',
        data: live_data,
        methods: {
            price_filter: price_filter,
            timeline_str: function () {
                var t = new Date().getTime();
                t = parseInt(t / 1000 / 60 / 3);
                return "?t=" + t;
            },
            open_order_info: function (order_id) {

                location.href = 'user_goods_orders.html?goods_order_id=' + order_id;
            },
            go_goods_detail: function (goods_id) {
                location.href = 'goods.html?goods_id=' + goods_id
            },
            cart_pay: function (goods_ids) {
                var cart_list_ids = [];
                $.each(goods_ids, function (index, goods_id) {
                    $.each(live_data.cart_list, function (index, c_goods) {
                        if (c_goods.goods_id == goods_id) {
                            cart_list_ids.push(c_goods.id)
                        }
                    })
                })
                api.lorder.create_order({cart_list: cart_list_ids.join(',')}).then(function (data) {
                    var payorder_id = data.result.id;
                    location.href = './pay.html?payorder_id=' + payorder_id
                })
            },
            delete_cart: function (c_goods) {
                Magiclamp.mcConfirm("提示", "从购物删除商品？", null, function () {
                    api.lgoods.delete_carts({cart_id_list: c_goods.id}).then(function (data) {
                        Magiclamp.mcAlert("提示", data.message, null, function () {
                            c_goods.is_active = false;
                            live_data.is_all = false;
                            checkAll();
                        });

                    });
                });
            },
            pop_num: function (c_goods) {
                c_goods.num -= 1;
                var goods_id = c_goods.goods_id;
                api.lgoods.update_cart({goods_id: goods_id, num: c_goods.num}).then(function (data) {
                })
            },
            add_num: function (c_goods) {
                c_goods.num += 1;
                var goods_id = c_goods.goods_id;
                api.lgoods.update_cart({goods_id: goods_id, num: c_goods.num}).then(function (data) {
                })
            },
            hide_live_list: function () {
                baseConfig.page_back();

            },
            play:function(imgid){
                var test_video = document.getElementById("v"+imgid);
                width = test_video.style.width;
                height = test_video.style.height;
                test_video.style.width = screen.width + "px";
                test_video.style.height = screen.height + "px";
            },
            pause:function(imgid){
                var test_video = document.getElementById("v"+imgid);
                test_video.style.width = width;
                test_video.style.height = height;
            },
            go_chat_list: function () {
                try {
                    client_storage("page", "tab_chat");
                } catch (error) {

                }
                location.href = 'home.html';
            },
            checkAll: function () {
                checkAll();
            },
            price_all: function (goods_ids) {
                var prices = 0;
                $.each(goods_ids, function (index, goods_id) {
                    $.each(live_data.cart_list, function (index, c_goods) {
                        if (c_goods.goods_id == goods_id) {
                            prices += c_goods.goods__price * c_goods.num
                        }
                    });
                });
                return live_data.cart_list.prices = prices
            },
            shengyudays: function (plan_start_time, live_status) {
                if (live_status == 7) {
                    return "正在直播";
                } else if (!plan_start_time) {
                    return "等待直播";
                } else {
                    var start_time = moment(plan_start_time);
                    var now = moment();
                    if (start_time.isBefore(now)) {

                        return "等待直播";
                    }
                    var days = start_time.diff(now, 'days');
                    start_time = start_time.subtract(days, 'days');
                    var hours = start_time.diff(now, 'hours');
                    start_time = start_time.subtract(hours, 'hours');
                    var minutes = start_time.diff(now, 'minutes');
                    if (days == 0) {
                        return "(剩余：" + now.subtract(hours).hours() + "小时)"
                    } else if (days < 0) {
                        return "等待直播";
                    } else {
                        return "(剩余：" + days + "天" + hours + '小时' + minutes + '分)';
                    }
                }

            },
            testPlay: function () {
                //Magiclamp.mcAlert("提示", "testPlay");
                if (live_data.prgoram.live_status != 7) {
                    return;
                }

                if (live_data.show_img) {
                    //Magiclamp.mcAlert("提示", "1");
                    if (live_data.type == 'android') {
                        var play_background = document.getElementById("play_background");
                        play_background.style.top = '50px';
                        window.test_video.addEventListener("x5videoexitfullscreen", function () {
                            //Magiclamp.mcAlert("提示", "3");
                            var play_background = document.getElementById("play_background");
                            play_background.style.top = '0px';
                            live_data.show_img = true;
                        });
                    }

                    window.test_video.play();
                    // document.getElementById("play_background").style.top="50px";

                } else {
                    //Magiclamp.mcAlert("提示", "2");
                    window.test_video.pause();
                    // play_background.style.top = '0px';
                }


                live_data.show_img = !live_data.show_img;
            },
            create_follow: function () {
                api.sys.create_followstore({goodsstore_id: live_data.goodsstore_id}).then(function (data) {
                    live_data.is_follow = true;
                    Magiclamp.mcAlert("提示", data.message, null, function () {
                    });
                })
            },
            delete_follow: function () {
                api.sys.delete_followstore({goodsstore_id: live_data.goodsstore_id}).then(function (data) {
                    live_data.is_follow = false;
                    Magiclamp.mcAlert("提示", data.message, null, function () {
                    });
                })
            },
        }
    });

    live_data.sessionid = GetQueryString('sessionid');
    if (live_data.sessionid) {
        live_data.sessionid_q = "sessionid=" + live_data.sessionid;
        client_storage('sessionid', live_data.sessionid);
    }


    document.getElementsByTagName("BODY")[0].onpageshow = function (event) {
        if (event && !event.persisted) {
            return;
        }

        login_mogu(function () {

            api.lgoods.query_cart_list({live_program_id: live_data.live_program_id}).then(function (data) {
                live_data.cart_list = data.result.list;
                window.order_detail.checkAll();
            });


            api.lorder.query_maiuser_order_by_programe_list({live_program_id: live_data.live_program_id}).then(function (data) {
                // live_data.goods_order_list = data.result.list
                _(data.result.list).each(function (item) {
                    live_data.goods_order_list.push(item);
                    item['goods_num'] = 0;
                    _(item.goods_order_item_list).each(function (goods) {
                        item['goods_num'] += goods.num;
                    });
                });
            });
        }, live_data.type, live_data.live_program_id, true);

        api.lorder.query_video_and_img_by_goodsstore_list({
            goodsstore_id: live_data.goodsstore_id,
            page_size: 1
        }).then(function (data) {
            live_data.video_list = data.result.list;
        })
        api.sys.get_followstore({goodsstore_id: live_data.goodsstore_id}).then(function (data) {
            live_data.is_follow = data.result.is_follow;
        })
    };
    document.getElementsByTagName("BODY")[0].onpageshow();


});

function go_home() {
    location.href = "./home.html"
}
